package com.tange.feature.device.query;

import androidx.annotation.NonNull;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.tange.base.toolkit.StringUtils;
import com.tange.base.toolkit.TGThreadPool;
import com.tange.core.backend.service.api.CoreBackendService;
import com.tange.core.data.structure.Response;
import com.tange.module.camera.query.CoreDevicePaginationResp;
import com.tg.appcommon.android.TGLog;
import com.tg.data.bean.DeviceListResp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class QueryDeviceInfo {
    private final static String TAG = "QueryDeviceInfo";
    private final static String PARAM_PAGE_INDEX = "page";
    private final static String PARAM_PAGE_LIMIT = "limit";
    private final static String PARAM_NAME_FILTER = "name_filter";
    private final static String PARAM_DEVICE_UUID = "uuid";

    public static void queryDeviceInfo(List<String> list, @NonNull final QueryDeviceInfoListener listener){
        int limit = list.size();
        if (limit > 0){
            TGThreadPool.execute(() -> {
                String deviceUuid = StringUtils.join(list, ",");
                HashMap<String, String> hashMap = new HashMap<>();
                hashMap.put(PARAM_PAGE_LIMIT, String.valueOf(limit));
                hashMap.put(PARAM_PAGE_INDEX, "1");
                hashMap.put(PARAM_DEVICE_UUID, deviceUuid);
                hashMap.put(PARAM_NAME_FILTER, "");
                CoreBackendService.api().getDeviceBasicList(hashMap).subscribe(new Observer<Response<JsonObject>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Response<JsonObject> jsonObjectResponse) {
                        TGLog.i(TAG, "onNext: " + jsonObjectResponse);
                        if (jsonObjectResponse != null && jsonObjectResponse.getData() != null) {
                            JsonObject jsonObject = jsonObjectResponse.getData();
                            TGLog.i(TAG, "jsonObjectResponse.getData(): " + jsonObject);
                            if (jsonObject != null && jsonObject.has("list")){
                                DeviceListResp deviceListResp = new DeviceListResp();
                                List<DeviceListResp.Device> list1 = new ArrayList<>();
                                JsonArray jsonArray = jsonObject.getAsJsonArray("list");
                                for (int i = 0; i < jsonArray.size(); i++) {
                                    JsonObject jsonObject1 = jsonArray.get(i).getAsJsonObject();
                                    DeviceListResp.Device device = new DeviceListResp.Device();
                                    TGLog.i(TAG, "jsonObject1: " + jsonObject1);
                                    if (jsonObject1 != null && jsonObject1.has("uuid")){
                                        String uuid = jsonObject1.get("uuid").getAsString();
                                        TGLog.i(TAG, "uuid: " + uuid);
                                        device.setUuid(uuid);
                                    }

                                    if (jsonObject1 != null && jsonObject1.has("id")){
                                        device.setId(jsonObject1.get("id").getAsLong());
                                    }

                                    if (jsonObject1 != null && jsonObject1.has("timezone")){
                                        device.setTimeZone(jsonObject1.get("timezone").getAsString());
                                    }
                                    list1.add(device);
                                }
                                deviceListResp.setList(list1);
                                listener.onCallBack(deviceListResp);
                            }

                        }

                    }

                    @Override
                    public void onError(Throwable e) {
                        TGLog.i(TAG, "getMessage: " + e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        TGLog.i(TAG, "onComplete:");
                    }
                });
            });
        }
    }


    public interface QueryDeviceInfoListener {
        void onCallBack(DeviceListResp deviceListResp);
    }
}
